﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Diagnostics;

namespace LogViewApp.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service
    {
        static XElement xStatus = new XElement("Status");

        [OperationContract]
        public void DoWork()
        {
            // Add your operation implementation here
            return;
        }

        [OperationContract]
        public DateTime GetServerTime()
        {
            // Add your operation implementation here
            return DateTime.Now;
        }

        [OperationContract]
        public string GetStatusUpdate()
        {
            // Add your operation implementation here
            //return DateTime.Now.ToLongTimeString();
            AddStatus(new XElement("Time", DateTime.Now.ToLongTimeString()));
            XElement xRet = xStatus;
            ResetStatus();
            return xRet.ToString(SaveOptions.DisableFormatting);
        }

        [OperationContract]
        public string UploadLogList(List<LogItem> listOfLogItem)
        {
            Debug.WriteLine("Got list of log items, total count is " + listOfLogItem.Count());
            return "Got list of log items, total count is " + listOfLogItem.Count();
        }

        [OperationContract]
        public string UploadOneLog(LogItem log)
        {
            Debug.WriteLine("Got one log at server side: log is " + log.Time);
            return log.Time;
        }

        static public void ResetStatus()
        {
            xStatus = new XElement("Status");
        }

        static public void AddStatus(XElement newone)
        {
            xStatus.Add(newone);
        }



        // Add more operations here and mark them with [OperationContract]
    }

    [DataContract]
    [Serializable]
    public class LogItem
    {
        [DataMember]
        public string error { get; set; }
        [DataMember]
        public string Sender { get; set; }
        [DataMember]
        public string Level { get; set; }
        [DataMember]
        public string Time { get; set; }
        [DataMember]
        public string Message { get; set; }
        [DataMember]
        public string WholeLog { get; set; }
    }
}
